package com.luych.toolbox.common.service.configuration;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class SwaggerConfiguration {

    @Bean
    public Contact contact(Environment environment) {
        String authorName = environment.getProperty("author.name");
        String authorUrl = environment.getProperty("author.url");
        String authorEmail = environment.getProperty("author.email");
        return new Contact(authorName, authorUrl, authorEmail);
    }

    @Bean
    public Docket docket(@Value("${spring.application.name}") String application, Contact contact) {
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        ApiInfo apiInfo = apiInfoBuilder
                .title(application.toUpperCase())
                .version("LATEST")
                .contact(contact)
                .build();

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo)
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }
}
